<!--

    Copyright © 2016-2025 The Thingsboard Authors

    Licensed under the Apache License, Version 2.0 (the "License");
    you may not use this file except in compliance with the License.
    You may obtain a copy of the License at

        http://www.apache.org/licenses/LICENSE-2.0

    Unless required by applicable law or agreed to in writing, software
    distributed under the License is distributed on an "AS IS" BASIS,
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    See the License for the specific language governing permissions and
    limitations under the License.

-->
<section [formGroup]="baseHttpIntegrationConfigForm">
  <section formGroupName="clientConfiguration">
    @if (!isNew) {
      <ng-container *ngTemplateOutlet="topicFilters"></ng-container>
    }
    <section class="pb-6" tb-hint-tooltip-icon="{{ 'integration.send-only-payload-hint' | translate }}">
      <mat-slide-toggle formControlName="sendOnlyMsgPayload">
        <span translate>integration.send-only-payload</span>
      </mat-slide-toggle>
    </section>
    <mat-form-field class="mat-block" appearance="outline">
      <mat-label translate>integration.endpoint-url</mat-label>
      <input matInput formControlName="restEndpointUrl" required>
      <tb-copy-button matSuffix
                      [class.!hidden]="!clientConfigurationFormGroup.get('restEndpointUrl')?.value?.length"
                      [copyText]="clientConfigurationFormGroup.get('restEndpointUrl')?.value">
      </tb-copy-button>
      @if (clientConfigurationFormGroup.get('restEndpointUrl').hasError('required')) {
        <mat-icon matSuffix
                  matTooltipClass="tb-error-tooltip"
                  matTooltip="{{ 'integration.endpoint-url-required' | translate }}"
                  class="tb-error">
          warning
        </mat-icon>
      }
      @if (clientConfigurationFormGroup.get('restEndpointUrl').hasError('privateNetwork')) {
        <mat-icon matSuffix
                  matTooltipClass="tb-error-tooltip"
                  matTooltip="{{ 'integration.url-private' | translate }}"
                  class="tb-error">
          warning
        </mat-icon>
      }
    </mat-form-field>
    <mat-form-field class="mat-block" appearance="outline">
      <mat-label translate>integration.request-method</mat-label>
      <mat-select formControlName="requestMethod">
        @for (requestType of httpRequestTypes; track requestType) {
          <mat-option [value]="requestType">
            {{ requestType }}
          </mat-option>
        }
      </mat-select>
    </mat-form-field>
    <section class="mb-4 flex flex-col gap-3">
      <tb-integration-credentials formControlName="credentials" [passwordOptional]="false"
                                  [allowCredentialTypes]="[IntegrationCredentialType.Anonymous,
                                                           IntegrationCredentialType.Basic,
                                                           IntegrationCredentialType.CertPEM]">
      </tb-integration-credentials>
      <section class="security-setting mat-padding">
        <p translate>integration.headers</p>
        <tb-header-filter-map
          class="security-setting-content"
          noDataText="integration.no-headers-filter"
          keyPlaceholderText="integration.header"
          isStrokedButton="true"
          formControlName="headers">
        </tb-header-filter-map>
      </section>
    </section>
    <ng-container [ngTemplateOutlet]="executeRemotelyTemplate()">
    </ng-container>
    <mat-expansion-panel class="configuration-panel">
      <mat-expansion-panel-header>
        <mat-panel-description class="flex items-stretch justify-end" translate>
          integration.advanced-settings
        </mat-panel-description>
      </mat-expansion-panel-header>
      <ng-template matExpansionPanelContent>
        <mat-form-field class="mat-block" appearance="outline">
          <mat-label translate>integration.select-content-type</mat-label>
          <mat-select formControlName="payloadContentType">
            @for (value of contentTypes; track value) {
              <mat-option [value]="value">
                <span>{{ contentTypeTranslation(value) | translate }}</span>
              </mat-option>
            }
          </mat-select>
          <mat-icon matSuffix matTooltip="{{ 'integration.select-content-type-hint' | translate }}">
            help
          </mat-icon>
        </mat-form-field>
        @if (!isBinaryContentType) {
          <section class="pb-6" tb-hint-tooltip-icon="{{ 'integration.send-binary-on-failure-hint' | translate }}">
            <mat-slide-toggle formControlName="sendBinaryOnParseFailure">
              <span translate>integration.send-binary-on-failure</span>
            </mat-slide-toggle>
          </section>
        }
        <mat-form-field class="mat-block" appearance="outline">
          <mat-label translate>integration.read-timeout</mat-label>
          <input type="number" step="1" min="0" matInput formControlName="readTimeoutMs">
          <mat-icon matSuffix matTooltip="{{ 'integration.read-timeout-hint' | translate }}">
            help
          </mat-icon>
        </mat-form-field>
        <mat-form-field class="mat-block" appearance="outline">
          <mat-label translate>integration.max-parallel-requests-count</mat-label>
          <input type="number" step="1" min="0" matInput formControlName="maxParallelRequestsCount">
          <mat-icon matSuffix matTooltip="{{ 'integration.max-parallel-requests-count-hint' | translate }}">
            help
          </mat-icon>
        </mat-form-field>
        <mat-form-field class="mat-block" appearance="outline">
          <mat-label translate>integration.max-response-size</mat-label>
          <input type="text" min="1" [max]="MemoryBufferSizeInKbLimit" inputmode="numeric" pattern="[0-9]*" matInput formControlName="maxInMemoryBufferSizeInKb">
          @if (clientConfigurationFormGroup?.get('maxInMemoryBufferSizeInKb').hasError('min') ||
               clientConfigurationFormGroup?.get('maxInMemoryBufferSizeInKb').hasError('max')) {
            <mat-icon matSuffix
                      matTooltipClass="tb-error-tooltip"
                      matTooltip="{{ 'integration.memory-buffer-size-range' | translate: { max: MemoryBufferSizeInKbLimit } }}"
                      class="tb-error">
              warning
            </mat-icon>
          }
          <mat-icon matSuffix matTooltip="{{ 'integration.max-response-size-hint' | translate }}">
            help
          </mat-icon>
        </mat-form-field>
        <ng-container [ngTemplateOutlet]="genericAdditionalInfoTemplate()">
        </ng-container>
      </ng-template>
    </mat-expansion-panel>
  </section>
  <ng-template #topicFilters>
    <section class="mb-6" [formGroup]="baseHttpIntegrationConfigForm">
      <section class="security-setting mat-padding">
        <p translate>integration.topic-filters</p>
        <tb-http-topic-filters [isEdit]="isEdit()" [integration]="integration()" formControlName="topicFilters">
        </tb-http-topic-filters>
      </section>
    </section>
  </ng-template>
</section>
